package com.ooqiu.gaming.components.retrofit;


import com.alibaba.fastjson.JSON;
import com.ooqiu.gaming.api.base.BaseResponse;

import io.reactivex.functions.Function;

/**
 * @author Eenie
 * @date 2017/11/21
 */

public class ConvertData<T> implements Function<BaseResponse, T> {
    private String filed;
    private Class clz;

    public ConvertData(Class clz) {
        this("");
        this.clz = clz;
    }
    public ConvertData() {
        this("");
    }


    public ConvertData(String filed) {
        this.filed = filed;
    }

    @Override
    public T apply(BaseResponse baseResponse) throws Exception {

        if (clz == null) {
            return (T) baseResponse.getData();
        }
        return (T) JSON.parseObject(baseResponse.getData(), clz);
    }
}
